package other;

/**
 * 0-1 背包问题
 */
public class BeiBaoProblem {

    public static void main(String[] args) {
        int volume = 8; //背包体积
        int count = 4; //物品数量

        int[] volumes = {0, 2, 3, 4, 5};
        int[] values = {0, 3, 4, 5, 6,};

        int[][] dp = new int[count + 1][volume + 1];

        for (int i = 0; i < volume; i++) {
            dp[0][i] = 0;
        }

        for (int i = 0; i < count; i++) {
            dp[i][0] = 0;
        }

        for (int j = 1; j <= volume; j++) {
            for (int i = 1; i <= count; i++) {
                if ((j - volumes[i]) < 0) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - volumes[i]] + values[i]);
                }

            }
        }

        for (int i = 0; i <= count; i++) {
            for (int j = 0; j <= volume; j++) {
                System.out.print(dp[i][j] + "   ");
            }
            System.out.println();
        }

    }
}
